| Conditions | 7 |
| Paths | 12 |
| Total Lines | 60 |
| Lines | 0 |
| Ratio | 0 % |
| Changes | 1 | ||
| Bugs | 1 | Features | 0 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
| 1 | import { List, Map, fromJS } from 'immutable'; |
||
| 3 | export const treeToFlatList = ( |
||
| 4 | data, |
||
| 5 | rootIdentifier = 'root', |
||
| 6 | childIdentifier = 'children' |
||
| 7 | ) => { |
||
| 8 | |||
| 9 | if (!data) { |
||
| 10 | throw new Error('Expected data to be defined'); |
||
| 11 | } |
||
| 12 | |||
| 13 | const result = []; |
||
| 14 | const cfg = { flatIndex: 0 }; |
||
| 15 | let stack = List(); |
||
| 16 | |||
| 17 | if (!Map.isMap(data)) { |
||
| 18 | data = fromJS(data); |
||
| 19 | } |
||
| 20 | |||
| 21 | if (data.get(rootIdentifier)) { |
||
| 22 | data = data.get(rootIdentifier); |
||
| 23 | |||
| 24 | stack = stack.push( |
||
| 25 | toItem(List(), childIdentifier, cfg)(data) |
||
| 26 | ); |
||
| 27 | } |
||
| 28 | else { |
||
| 29 | stack = data.get(childIdentifier).map( |
||
| 30 | toItem(List([-1]), List([0]), childIdentifier) |
||
| 31 | ); |
||
| 32 | } |
||
| 33 | |||
| 34 | while (stack.count()) { |
||
| 35 | |||
| 36 | const item = stack.first(); |
||
| 37 | const children = item.get(childIdentifier); |
||
| 38 | |||
| 39 | stack = stack.shift(); |
||
| 40 | |||
| 41 | if (List.isList(children) && !item.get('_hideChildren')) { |
||
| 42 | stack = children.map( |
||
| 43 | toItem( |
||
| 44 | item.get('_path').push(item.get('_id')), |
||
| 45 | childIdentifier, |
||
| 46 | cfg, |
||
| 47 | item, |
||
| 48 | children |
||
| 49 | ) |
||
| 50 | ).concat(stack); |
||
| 51 | } |
||
| 52 | |||
| 53 | // removing erroneous data since grid uses internal values |
||
| 54 | result.push( |
||
| 55 | item.delete(childIdentifier) |
||
| 56 | .delete('parentId') |
||
| 57 | .delete('id') |
||
| 58 | ); |
||
| 59 | } |
||
| 60 | |||
| 61 | return List(result); |
||
| 62 | }; |
||
| 63 | |||
| 109 |